package com.example.dao;

import com.example.entity.Salary;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 工资数据访问对象，提供工资相关数据库操作
 */
public class SalaryDao extends BaseDao {
    private static final Logger LOGGER = Logger.getLogger(SalaryDao.class.getName());

    /**
     * 添加工资记录
     */
    public int addSalary(Salary salary) {
        String sql = "INSERT INTO salaries (employee_id, month, base_salary, bonus, deduction, status) VALUES (?, ?, ?, ?, ?, ?)";
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, salary.getEmployeeId());
            pstmt.setString(2, salary.getMonth());
            pstmt.setDouble(3, salary.getBaseSalary());
            pstmt.setDouble(4, salary.getBonus());
            pstmt.setDouble(5, salary.getDeduction());
            pstmt.setString(6, salary.getStatus());

            return pstmt.executeUpdate();
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to add salary record", e);
            return 0;
        } finally {
            close(conn, pstmt, null);
        }
    }

    /**
     * 获取所有工资记录
     */
    public List<Salary> getAllSalaries() {
        List<Salary> salaries = new ArrayList<>();
        String sql = "SELECT s.*, e.name AS employee_name, d.name AS department_name " +
                "FROM salaries s " +
                "JOIN employees e ON s.employee_id = e.employee_id " +
                "JOIN department d ON e.department_id = d.id";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                Salary salary = new Salary();
                salary.setId(rs.getInt("salary_id"));
                salary.setEmployeeId(rs.getInt("employee_id"));
                salary.setEmployeeName(rs.getString("employee_name"));
                salary.setDepartmentName(rs.getString("department_name"));
                salary.setMonth(rs.getString("month"));
                salary.setBaseSalary(rs.getDouble("base_salary"));
                salary.setBonus(rs.getDouble("bonus"));
                salary.setDeduction(rs.getDouble("deduction"));
                salary.setNetSalary(rs.getDouble("net_salary"));
                salary.setStatus(rs.getString("status"));
                salary.setPaymentDate(rs.getString("payment_date"));

                salaries.add(salary);
            }
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to get all salary records", e);
        } finally {
            close(conn, stmt, rs);
        }

        return salaries;
    }

    /**
     * 根据ID获取工资记录
     */
    public Salary getSalaryById(int id) {
        Salary salary = null;
        String sql = "SELECT s.*, e.name AS employee_name, d.name AS department_name " +
                "FROM salaries s " +
                "JOIN employees e ON s.employee_id = e.employee_id " +
                "JOIN department d ON e.department_id = d.id WHERE s.salary_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                salary = new Salary();
                salary.setId(rs.getInt("salary_id"));
                salary.setEmployeeId(rs.getInt("employee_id"));
                salary.setEmployeeName(rs.getString("employee_name"));
                salary.setDepartmentName(rs.getString("department_name"));
                salary.setMonth(rs.getString("month"));
                salary.setBaseSalary(rs.getDouble("base_salary"));
                salary.setBonus(rs.getDouble("bonus"));
                salary.setDeduction(rs.getDouble("deduction"));
                salary.setNetSalary(rs.getDouble("net_salary"));
                salary.setStatus(rs.getString("status"));
                salary.setPaymentDate(rs.getString("payment_date"));
            }
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to get salary record by id", e);
        } finally {
            close(conn, pstmt, rs);
        }

        return salary;
    }

    /**
     * 删除工资记录
     */
    public void deleteSalary(int id) {
        String sql = "DELETE FROM salaries WHERE salary_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to delete salary record", e);
        } finally {
            close(conn, pstmt, null);
        }
    }

    /**
     * 发放工资
     */
    public void paySalary(int id) {
        String sql = "UPDATE salaries SET status = 'paid', payment_date = CURDATE() WHERE salary_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to pay salary", e);
        } finally {
            close(conn, pstmt, null);
        }
    }

    /**
     * 更新工资记录
     */
    public void updateSalary(Salary salary) {
        String sql = "UPDATE salaries SET employee_id = ?, month = ?, base_salary = ?, bonus = ?, deduction = ?, status = ? WHERE salary_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, salary.getEmployeeId());
            pstmt.setString(2, salary.getMonth());
            pstmt.setDouble(3, salary.getBaseSalary());
            pstmt.setDouble(4, salary.getBonus());
            pstmt.setDouble(5, salary.getDeduction());
            pstmt.setString(6, salary.getStatus());
            pstmt.setInt(7, salary.getId());

            pstmt.executeUpdate();
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "Failed to update salary record", e);
        } finally {
            close(conn, pstmt, null);
        }
    }
}